package beans;



import entities.Client;
import javax.el.ELResolver;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Mirek
 */


@ManagedBean(name="sessionManager")
@ApplicationScoped
public class SessionManager {
    private FacesContext ctx;
    private ELResolver er;
    private static SessionManager instance;
    private SessionManager()
    {
        
    }
    public static SessionManager getInstance()
    {
        if(instance == null)
            instance = new SessionManager();
        instance.init();
        return instance;
    }
    private FacesContext init()
    {
        ctx = FacesContext.getCurrentInstance();
        er = ctx.getApplication().getELResolver();
        return ctx;
    }
    public Object getSessionItem(String name)
    {
        init();
        return er.getValue(ctx.getELContext(),null, name);
    }
    public Client getCurrentClient()
    {
        //return (Client)getSessionItem("currentClient");
        return (Client)getSessionItem("client");
    }
    public boolean getIsLogged(){
        return false;
    }
    public  void setSessionItem(String name, Object item)
    {
        ctx.getExternalContext().getSessionMap().put( name,item );
    }
    public void clearSessionItem(String name)
    {
        ctx.getExternalContext().getSessionMap().put(name, null);

    }
    public void setRequestItem(String name, Object item)
    {
        ctx.getExternalContext().getRequestMap().put(name, item);

    }
    public void setFlashMessage(String type, String message)
    {
        FacesMessage msg = new FacesMessage(message);
        ctx.addMessage(type,msg);
    }

}
